package com.j3l11234.familyvote.core;



import java.security.MessageDigest;
import java.util.ArrayList;
import java.util.List;
import java.util.TimerTask;

import org.apache.http.Consts;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;




public class Diaoyu {
	private long totalVote;

	private HttpPost vip_tip;
	private int proxyListIndex;

	public Diaoyu() {

		vip_tip = new HttpPost("http://a66ee9df51cf7c5b.cdn.jiashule.com./12/moban/vip_tip%5B1%5D_(1).css.asp");
		Diaoyu.setHeader(vip_tip);


	}

	public void start() {
		for (long j = 0; j < 10; j++) {
			new Thread(voteRun).start();
		}

		new java.util.Timer().scheduleAtFixedRate(new TimerTask() {
			private long last;
			public void run() {
				long totalVote2 = totalVote;
				System.out.println(totalVote2+","+(totalVote2-last)/10);
				last = totalVote2;
			}
		},0, 10000);
	}

	Runnable voteRun = new Runnable() {
		public void run() {
			CloseableHttpClient httpclient = HttpClients.createDefault();
			while (true) {
				try {
					for (int j = 0; j < 5; j++) {
						List<NameValuePair> formparams = new ArrayList<NameValuePair>();
						String u = String.valueOf((long)(Math.random()*8888888888L+100000000));
						formparams.add(new BasicNameValuePair("u", u));
						formparams.add(new BasicNameValuePair("p", MD5(u).substring(0, 10)));
						formparams.add(new BasicNameValuePair("verifycode", ""));
						formparams.add(new BasicNameValuePair("id", "329"));	
						formparams.add(new BasicNameValuePair("aid", "15004501"));
						formparams.add(new BasicNameValuePair("u1", "http://ctc.qzs.qq.com/ac/qzone/login/succ.html?para=toolbar"));
						formparams.add(new BasicNameValuePair("fp", "http://ctc.qzs.qq.com/ac/qzone/login/error.html"));
						formparams.add(new BasicNameValuePair("h", "1"));
						formparams.add(new BasicNameValuePair("ptredirect", "0"));
						formparams.add(new BasicNameValuePair("ptlang", "2052"));
						formparams.add(new BasicNameValuePair("from_ui", "1"));
						formparams.add(new BasicNameValuePair("dumy", ""));
						UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, Consts.UTF_8);
						vip_tip.setEntity(entity);
						vip_tip.setHeader("Cookie", "ASPSESSIONIDCABAQDQA="+MD5(u));
						CloseableHttpResponse voteResponse = httpclient.execute(vip_tip);
						String retult = EntityUtils.toString(voteResponse.getEntity());
						System.out.println(retult);
						voteResponse.close();  
					}
					synchronized (this) {
						totalVote += 5;
					}
				}catch(Exception e){
					System.err.println(e.getClass().getName()+","+e.getMessage());
				}

			}
		}
	};

	public static void setHeader(AbstractHttpMessage httpGet){
		httpGet.setHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
		httpGet.setHeader("Accept-Encoding", "gzip, deflate");
		httpGet.setHeader("Accept-Language", "zh-CN,zh;q=0.8");
		httpGet.setHeader("Cache-Control", "max-age=0");
		httpGet.setHeader("Connection", "keep-alive");
		httpGet.setHeader("Host", "a66ee9df51cf7c5b.cdn.jiashule.com.");
		httpGet.setHeader("Origin", "http://a66ee9df51cf7c5b.cdn.jiashule.com.");
		httpGet.setHeader("Referer", "http://a66ee9df51cf7c5b.cdn.jiashule.com./12/moban/1/-329.htm");
		httpGet.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36");
	}

	public final static String MD5(String s) {
		char hexDigits[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};       
		try {
			byte[] btInput = s.getBytes();
			// 获得MD5摘要算法的 MessageDigest 对象
			MessageDigest mdInst = MessageDigest.getInstance("MD5");
			// 使用指定的字节更新摘要
			mdInst.update(btInput);
			// 获得密文
			byte[] md = mdInst.digest();
			// 把密文转换成十六进制的字符串形式
			int j = md.length;
			char str[] = new char[j * 2];
			int k = 0;
			for (int i = 0; i < j; i++) {
				byte byte0 = md[i];
				str[k++] = hexDigits[byte0 >>> 4 & 0xf];
				str[k++] = hexDigits[byte0 & 0xf];
			}
			return new String(str);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
	}

	public static void main(String[] args) {
		Diaoyu  fv = new Diaoyu();
		fv.start();
	}
}